#!/bin/bash
# create code dir
[[ -d ${CODE_DIR}/${env} ]] || mkdir -p ${CODE_DIR}/${env}
[[ -d ${CODE_DIR}/fe/${env} ]] || mkdir ${CODE_DIR}/fe/${env}

# set git
if [[ ${deploy_type} == "static" -o ${deploy_type} == "node" ]];then
  [[ -d ${CODE_DIR}/fe/${env}/${app} ]] && rm -rf ${CODE_DIR}/fe/${env}/${app}
  cd ${CODE_DIR}/fe/${env}
else
  [[ -d ${CODE_DIR}/${env}/${app} ]] && rm -rf ${CODE_DIR}/${env}/${app}
  cd ${CODE_DIR}/${env}
fi

git clone ${gitpath} ${app}
if [[ "${repo}" = "origin" ]]; then
    git checkout .
    git fetch origin ${branch}
    git checkout origin/${branch}
    git branch -D ${branch}
    git checkout -b ${branch} ${repo}/${branch}
    git pull
else
    git remote add  ${repo} ssh://git@git.dangdang.com/${repo}/${app}.git
    git fetch ${repo}
    git checkout .
    git checkout ${repo}/${branch}
    git branch -D ${branch}
    git checkout -b ${branch} ${repo}/${branch}
    git pull
fi